package cn.edu.nuc.bre.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.nuc.bre.dao.QueryMapper;
import cn.edu.nuc.bre.dao.TaskMapper;
import cn.edu.nuc.bre.dto.QueryHandler;
import cn.edu.nuc.bre.entity.Task;
import cn.edu.nuc.bre.service.interfaces.ChildTaskService;
import cn.edu.nuc.bre.util.BreInfo;
import cn.edu.nuc.bre.util.PageInfo;

/**
 * @author Mine
 * @since 2016年11月12日_下午4:25:36
 */
@Service
public class ChildTaskServiceImpl implements ChildTaskService{
	@Autowired
	private TaskMapper tm;
	@Autowired
	private QueryMapper qm;
	
	@Override
	public boolean add(Task one) {
		if (one != null) {
			one.setTid(null);// 不需要id
			one.setTstate(0);// 设置初始状态,待响应
			if (tm.insertSelective(one) == 1) {
				return true;
			} else {
				System.out.println("数据库添加操作失败...");
			}
		} else {
			System.out.println("子任务参数为空...");
		}
		return false;
	}

	@Override
	public Task findById(Integer taskid) {
		if (taskid != null) {
			return tm.selectByPrimaryKey(taskid);
		} else {
			System.out.println("id为空");
			return null;
		}
	}
	
	@Override
	public PageInfo<Task> findByQueryHandler(QueryHandler qh) {
		List<Task> list = qm.queryTask(qh);
		long rootSize = 0;
		int nowPage = 1;
		int pageSize = 5;
		if (qh == null || qh.getLimitSentence() == null) {// 没分页
			if (list != null) {
				rootSize = list.size();
			}
		} else {
			nowPage = qh.getPageNumber();
			pageSize = qh.getPageSize();
			rootSize = qm.findLastRows();
		}
		/*
		 * System.out.println(rootSize); System.out.println(nowPage);
		 * System.out.println(pageSize);
		 */
		PageInfo<Task> pi = new PageInfo<Task>((int) rootSize, nowPage, pageSize);
		// System.out.println(pi);
		pi.setBasePageList(list);
		return pi;
	}


	@Override
	public boolean editState(Integer taskid, Integer stateCode) {
		//删除状态 -1 初始状态 【待响应】0 , 处理中 1   处理完成 2
		if (taskid != null && stateCode!=null) {
			Task one=new Task();
			one.setTaskid(taskid);
			switch (stateCode) {
			case BreInfo.CHILD_TASK_DELETE: one.setTstate(-1);
				break;
			case BreInfo.CHILD_TASK_RESPOND_PROCESS: one.setTstate(0);
				break;
			case BreInfo.CHILD_TASK_PROCESS: one.setTstate(1);
				break;
			case BreInfo.CHILD_TASK_END: one.setTstate(2);
					one.setTdealtime(new Date());//设置处理时间
				break;
			default:
				System.out.println("错误的状态码...");
				return false;//返回失败
			}
			if (tm.updateByPrimaryKeySelective(one) == 1) {
				return true;
			} else {
				System.out.println("子任务[修改状态]操作失败...");
				throw new RuntimeException("子任务[修改状态]操作失败...");
			}
		} else {
			System.out.println("参数不完整...");
		}
		return false;
	}

	@Override
	public boolean isFinishedByTid(Integer tid) {
		boolean flag=true;
		//先获取指定id所有子任务
		QueryHandler qh=QueryHandler.creat().andColumnMark("tid", "=", tid+"").set();
		PageInfo<Task> pageinfo = this.findByQueryHandler(qh);
		if(pageinfo==null||pageinfo.getBasePageList()==null
				||pageinfo.getBasePageList().size()<1){
			return false;
		}
		for (Task childTask : pageinfo.getBasePageList()) {
			if(childTask.getTstate()!=BreInfo.CHILD_TASK_END){
				flag=false;
				break;
			}
		}
		return flag;
	}

}
